package com.onlineMIS.ORM.DAO.headQ.finance;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.onlineMIS.ORM.DAO.BaseDAO;
import com.onlineMIS.ORM.entity.base.BaseOrder;
import com.onlineMIS.ORM.entity.headQ.finance.ExpenseHq;
import com.onlineMIS.common.Common_util;

@Repository
public class ExpenseHqImpl extends BaseDAO<ExpenseHq>{


	/**
	 * 根据卡号id,查询该卡在startTime和endTime 范围内的流水信息
	 * @param startTime
	 * @param endTime
	 * @return
	 */
	public List<ExpenseHq> selectFlowByCardId(int cardId, Date startTime, Date endTime) {
		DetachedCriteria criteria = DetachedCriteria.forClass(ExpenseHq.class);
		criteria.add(Restrictions.eq("status",BaseOrder.STATUS_COMPLETE));
		criteria.add(Restrictions.eq("financeCategory.id",cardId));
		criteria.add(Restrictions.between("expenseDate", startTime, endTime));
		List<ExpenseHq> expenseFlows =  getByCritera(criteria, false);
		return expenseFlows;
	}


	/**
	 * get the accumulate acct flow
	 * 
	 * @return
	 */
	public Map<Integer, Double> getAccumulateAcctFlowMap() {
		Map<Integer, Double> flowMap = new HashMap<Integer, Double>();
		String sql = "select financeCategory.id, sum(amount) from ExpenseHq where status = 2 group by financeCategory.id";

		List<Object> results_accu = this.executeHQLSelect(sql, new Object[] {}, null, true);

		if (results_accu != null) {
			for (Object object : results_accu) {
				Object[] oneRecord = (Object[]) object;
				int vipCardId = (Integer) oneRecord[0];
				Double amount = Common_util.getDouble(oneRecord[1]);
				flowMap.put(vipCardId, amount);
			}
		}

		return flowMap;
	}


}
